package com.ushine.web.component.design.chain.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * @Author: yls
 * @Date: 2021/12/28 18:33
 * @Description:
 * @Version 1.0
 */
@Service
public class ApplicationContextHelper implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }


    /**
     * 功能描述:
     * 〈 获取bean 〉
     *
     * @param clazz 反射类型
     * @return : T
     * @author : yls
     * @date : 2021/12/28 18:35
     */
    public <T> T popBean(Class<T> clazz) {
        if (applicationContext == null) {
            return null;
        }
        return applicationContext.getBean(clazz);
    }

    /**
     * 功能描述:
     * 〈 获取bean 〉
     *
     * @param name  类名称
     * @param clazz 类映射
     * @return : T
     * @author : yls
     * @date : 2021/12/28 18:36
     */
    public <T> T popBean(String name, Class<T> clazz) {
        if (applicationContext == null) {
            return null;
        }
        return applicationContext.getBean(name, clazz);
    }

}
